package cn.rcth.base.util;

import cn.rcth.base.exception.CustomException;
import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

//断言工具
public class Assert {

    //手机号验证码规则
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[0-9])|(18[0,0-9]))\\d{8}$";


    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new CustomException(message);
        }
    }
    //断言手机号不能为空
    public static void notNull(String str, String message) {
        if (!StringUtils.hasLength(str)) {
            throw new CustomException(message);
        }
    }

    // 断言范围长度
    public static void isRangLength(String str, int min ,int max , String message) {
        notNull(str,message);
        if(str.length() < min || str.length() > max){
            throw new CustomException(message);
        }
    }
    public static void isLength(String str, int lenth , String message) {
        notNull(str,message);
        if(str.length() != lenth){
            throw new CustomException(message);
        }
    }

    //断言手机号格式:正则表达式
    public static void isPhone(String phone, String errorMsg) {
        if(!Pattern.matches(REGEX_MOBILE, phone)){
            throw new CustomException(errorMsg);
        }

    }

    public static void isFalse(boolean b, String errorMsg) {
        if(!b){
            throw new CustomException(errorMsg);
        }
    }
}
